package com.example.java8.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * TODO
 *
 * @author qibao
 * @version v0.1 2020/11/20
 */
public class MethodTest {

    //    常用函数式接口
    public void interTest() {
//        Employee employee = new Employee("张三", 4, 4.4d);
//        Consumer<Employee> consumer = (x) -> System.out.println("consume");
//        consumer.accept(employee);
//        Supplier<Employee> supplier = () -> new Employee();
//        Supplier<Employee> supplier2 = Employee::new;
//        supplier2.get();
//        Function<String, Employee> function = (x) -> new Employee(x, 4, 10d);
//        String name = function.apply("李四").getName();
//        System.out.println(name);
//
//        Predicate<Employee> predicate = (x) -> employee.equals("张三");
//        boolean b = predicate.test(employee);
    }

    //    方法引用
    public void method0() {
        List list = new ArrayList();
        list.stream().forEach(System.out::println);
    }

    //    构造器引用
    // 构造器参数列表要和函数式接口参数列表一致
    public void testStructure0() {
        Supplier<Employee> supplier = () -> new Employee();
        supplier.get();
        Supplier<Employee> employeeSupplier = Employee::new;
        Employee employee = employeeSupplier.get();

        Function<Integer, Employee> employeeFunction = Employee::new;

    }

    //数组引用
//Type::new
    public void testArray() {
        Function<Integer, String[]> function = (x) -> new String[x];
        String string[] = function.apply(10);
        System.out.println(string.length);

        Function<Integer, String[]> function1 = String[]::new;
        String[] str2 = function1.apply(20);
        System.out.println(str2.length);
    }
}
